﻿using System;
using System.Xml.Linq;
using System.Linq;
using Transformable_Engine_v2.Engine.Input;

namespace Super_Management_Mayhem.Control
{

    public class InputKeys
    {
        public int GamepadId { get; set; }

        public KeyboardInput.Key UpKey { get; set; }
        public KeyboardInput.Key DownKey { get; set; }
        public KeyboardInput.Key LeftKey { get; set; }
        public KeyboardInput.Key RightKey { get; set; }
        public KeyboardInput.Key AttackKey { get; set; }
        public KeyboardInput.Key ActionKey { get; set; }

        public bool UpKeyState { get; set; }
        public bool DownKeyState { get; set; }
        public bool LeftKeyState { get; set; }
        public bool RightKeyState { get; set; }

        public static InputKeys FromFile(string fileName)
        {
            var xmlReader = XDocument.Load(fileName);

            var data = xmlReader.Descendants("Data").Descendants()
                .Select(x => new
                        {
                            Tag = x.Name,
                            Value = x.Value
                        }).ToDictionary(x => x.Tag, x => x.Value);

            InputKeys input = new InputKeys {
                                                GamepadId = Convert.ToInt32(data["GamepadId"]),
                                                UpKey = ParseKey(data["UpKey"]),
                                                LeftKey = ParseKey(data["LeftKey"]),
                                                DownKey = ParseKey(data["DownKey"]),
                                                RightKey = ParseKey(data["RightKey"]),
                                                AttackKey = ParseKey(data["AttackKey"]),
                                                ActionKey = ParseKey(data["ActionKey"]),
                                            };

            return input;
        }

        static KeyboardInput.Key ParseKey(string value)
        {
            return (KeyboardInput.Key) Enum.Parse(typeof(KeyboardInput.Key), value);
        }
    }
}